package cn.edu.nju.software.action.transaction;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.bean.show.BookItemInOrder;
import cn.edu.nju.software.bean.show.UserShow;
import cn.edu.nju.software.util.Constants;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FillOrderInfoAction extends ActionSupport implements SessionAware {
	
	private List<Integer> booksQuantity;
	private String address;
	private String formatAddress;
	private Map<String, Object> session;

	public List<Integer> getBooksQuantity() {
		return booksQuantity;
	}

	public void setBooksQuantity(List<Integer> booksQuantity) {
		this.booksQuantity = booksQuantity;
	}

	public String getAddress() {
		return address;
	}

	public String getFormatAddress() {
		return formatAddress;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public String execute() throws Exception {
		@SuppressWarnings("unchecked")
		List<BookItemInOrder> cart = (List<BookItemInOrder>) session.get(Constants.SESSION_CART);
		int itemNum = cart.size();
		for (int i = 0; i < itemNum; i++) {
			cart.get(i).setQuantity(booksQuantity.get(i));
		}

		address = ((UserShow) session.get(Constants.SESSION_USER)).getAddress();
		if (address != null) {
			formatAddress = address.replace(Constants.ADDRESS_SEPARATOR, "，");
		} else {
			formatAddress = "";
		}
		
		return SUCCESS;
	}

}
